package main

import (
	"flag"
	"fmt"
	"os"

	"rds-mysql-downloader/config"
	"rds-mysql-downloader/download"

	"github.com/go-kit/log"
	"github.com/go-kit/log/level"
)

const Version = "v1.1.1"

func main() {
	aliyun := flag.NewFlagSet("aliyun", flag.ExitOnError)
	create := aliyun.Bool("create", false, "run crate backup items")
	pull := aliyun.Bool("pull", false, "run pull backup items")
	status := aliyun.Bool("status", false, "get backup status")
	logger := log.NewLogfmtLogger(os.Stdout)
	logger = log.With(logger, "time", log.DefaultTimestamp)
	logger = log.With(logger, "caller", log.DefaultCaller)
	alBakcup := download.NewBackup(logger)
	if num := len(os.Args); num < 2 {
		aliyun.Usage()
		return
	}
	switch os.Args[1] {
	case "aliyun":
		if err := aliyun.Parse(os.Args[2:]); err == nil {
			if *create {
				level.Info(logger).Log("received action:", "create")
				createErr := alBakcup.Create(config.ConfigInfo.Keys.PublicKey, config.ConfigInfo.Keys.PrivateKey)
				if createErr != nil {
					fmt.Println("create backup items error: ", err)
					return
				}
			} else if *pull {
				level.Info(logger).Log("received action:", "pull")
				backupList := alBakcup.FetchBackupList(config.ConfigInfo.Keys.PublicKey, config.ConfigInfo.Keys.PrivateKey)
				alBakcup.Download(backupList)
			} else if *status {
				level.Info(logger).Log("received action:", "status")
				alBakcup.BackupJobStatus(config.ConfigInfo.Keys.PublicKey, config.ConfigInfo.Keys.PrivateKey)
			}
		}
	default:
		aliyun.Usage()
	}
}
